<?php
class MSql Extends CI_Model {

  /**
   * Auxiliar de Consulta
   */
  private $sCon = '';

  /**
   * Tabla Principal de Conexion
   */
  private $tabla = '';

  /**
   * Campos de Seleccion
   */
  private $set = '*';

  /**
   * Selecion Simple del Codigo
   */
  private $select = '';

  /**
   * Unir Varias Tablas
   */
  private $join = '';

  /**
   * Donde Elementos
   */
  private $where = '';
  
  /**
   * Grupo de Elementos
   */
  private $group = '';

  private $arr = array();

  private $param = 0;
  /**
   * Crear Select
   * @param Arreglos de Where
   *
   * @return string (SQL)
   */
  private function _getSelect() {

    $sCon = 'SELECT ' . $this -> set . ' FROM ' . $this -> tabla . ' ';
    return $sCon;
  }

  /**
   * Crear Set
   * @param Arreglos de Campos
   *
   * @return string (SQL)
   */
  private function _getSet($set = array()) {

  }

  /**
   * Crear Join
   * @param Tabla Inicial
   * @param Arreglos de las Uniones
   *
   * @return string (SQL)
   */
  private function _getJoin($tabla, $join = array()) {
    $sCon = '';
    $sQ = '';
    foreach ($join as $Clv => $Val) {
      $mdl = "T_" . $this -> param++;
      $this -> load -> model($Clv, $mdl);
      if (isset($this -> $mdl -> cmp['join'])) {
        if (count($this -> $mdl -> cmp['join']) > 0) {
          $sQ .= $this -> _getJoin($this -> $mdl -> tbl, $this -> $mdl -> cmp['join']);
        }
      }
      $sCon .= ' JOIN ' . $this -> $mdl -> tbl;
      foreach ($Val as $sC => $sV) {
        $sCon .= ' ON ' . $this -> $mdl -> tbl . '.' . $sC . '=' . $tabla . '.' . $sV;
      }
      unset($mdl);
    }
    return $sCon . $sQ;
  }

  /**
   * Crear Select
   * @param Arreglos de Where
   *
   * @return string (SQL)
   */
  private function _getWhere($where = array()) {

  }

  /**
   * Crear GROUP BY
   * @param Arreglos de Group
   *
   * @return string (SQL)
   */
  private function _getGroup($group = array()) {

  }

  /**
   * Generar Codigo SQL
   * @param Tabla Principal
   * @param Arreglo de Conexion
   *
   * @return string
   */
  function getSQL($tabla = '', $arr = array()) {
    $this -> arr = $arr;
    $this -> tabla = $tabla;

    $this -> sCon .= $this -> _getSet();
    $this -> sCon .= $this -> _getSelect();
    $this -> sCon .= $this -> _getJoin($tabla, $arr['join']);
    $this -> sCon .= $this -> _getWhere();

    return $this -> sCon;
  }

}
?>